package com.sunagy.demand.backend.controller;

import com.sunagy.demand.backend.VO.ResultVO;
import com.sunagy.demand.backend.VO.request.ProductAddRequestVO;
import com.sunagy.demand.backend.VO.request.ProductEditRequestVO;
import com.sunagy.demand.backend.entity.Product;
import com.sunagy.demand.backend.VO.response.ProductResponseVO;
import com.sunagy.demand.backend.service.ProductService;
import com.sunagy.demand.backend.utils.ResultVOUtil;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * desc：#TODO fill me ！
 * author：三餘無夢生
 * date：2018/4/27
 * time：下午12:52
 */
@Slf4j
@RestController
@RequestMapping("/product")
@Api(value = "管理端产品模块", description = "管理端产品模块")
public class ProductController {

    @Autowired
    protected ProductService productService;

    @ApiOperation(value = "查询产品列表", notes = "树形返回所有产品不分页")
    @ApiResponses({
            @ApiResponse(code = 500, message = "接口异常")
    })
    @RequestMapping(value = "/", method = RequestMethod.GET)
    public ResultVO<List<ProductResponseVO>> getTree() {
        List<ProductResponseVO> productResponseVOList = productService.getTree();
        return ResultVOUtil.success(productResponseVOList);
    }

    @ApiOperation(value = "查询产品列表", notes = "列表返回所有产品不分页")
    @ApiResponses({
            @ApiResponse(code = 500, message = "接口异常")
    })
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public ResultVO<List<ProductResponseVO>> getList() {
        List<ProductResponseVO> productResponseVOList = productService.getList();
        return ResultVOUtil.success(productResponseVOList);
    }

    @ApiOperation(value = "新增产品", notes = "新增一个产品")
    @ApiResponses({
            @ApiResponse(code = 500, message = "接口异常")
    })
    @RequestMapping(value = "", method = RequestMethod.POST)
    public ResultVO<ProductResponseVO> add(@RequestBody ProductAddRequestVO productAddRequestVO) {
        ProductResponseVO productResponseVO = productService.add(productAddRequestVO);
        return ResultVOUtil.success(productResponseVO);
    }

    @ApiOperation(value = "编辑产品", notes = "编辑一个产品")
    @ApiResponses({
            @ApiResponse(code = 500, message = "接口异常")
    })
    @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
    public ResultVO<ProductResponseVO> edit(@PathVariable Integer id, @RequestBody ProductEditRequestVO productEditRequestVO) {
        ProductResponseVO productResponseVO = productService.edit(id,productEditRequestVO);
        return ResultVOUtil.success(productResponseVO);
    }

    @ApiOperation(value = "删除产品", notes = "根据id删除一个产品")
    @ApiResponses({
            @ApiResponse(code = 500, message = "接口异常")
    })
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public ResultVO<Integer> del(@PathVariable Integer id) {
        Integer delId = productService.del(id);
        return ResultVOUtil.success(delId);
    }
}
